const api = require('../../utils/api.js');
const app = getApp();

Page({
  data: {
    selected: -1,
    disabled: false,
    money: '',
    unit: '',
    price: '',
    decs: '',
    useTabbar: !(app.tabbarindex === 0 && app.siteInfo.tabbar_type === '1'),
    is_show: true,
    abnorData: {
      imgSrc: app.imgUrl + 'abnor-19.png',
      text: '该功能已下架，暂不可用！',
    },
    isAuditMode: true,
  },

  onLoad(options) {
    let opts = options || {};
    app.pageOnLoad(this);
    let that = this;
    that.setData({ 
      opts,
      useTabbar: that.data.useTabbar && app.getTabBarStatus(),
    });
    if (opts.from === 'nianka') {
      wx.setNavigationBarTitle({
        title: 'VIP会员卡',
      });
    }
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    wx.showLoading({
      title: '加载中',
    });
    app.ws_micro_shoprequest({
      url: opts.from ? api.user.member_card_setting : api.recharge.list,
      success: function(res) {
        if (res.code === 0) {
          wx.setNavigationBarTitle({
            title: '充值',
          })
          let data = res.data;
          if (opts.from) {
            if (Number(data.member_card) === 0) {
              wx.showModal({
                title: '提示',
                content: 'VIP会员卡功能未开启，请联系管理员！',
                showCancel: false,
                success: function(res) {
                  if (res.confirm) {
                    app.util.navigateTo('/ws_micro_shop/pages/user/user');
                  }
                },
              });
            }
          } else {
            if (!data.balance || Number(data.balance.status) === 0) {
              wx.showModal({
                title: '提示',
                content: '充值功能未开启，请联系管理员！',
                showCancel: false,
                success: function(res) {
                  if (res.confirm) {
                    app.util.navigateTo('/ws_micro_shop/pages/user/user');
                  }
                },
              });
            }
            if (Number(data.balance.type) === 0) {
              that.setData({
                selected: -2,
              });
            }
          }
          that.setData(
            res.data,
          );
        } else if (res.code === 10001) {
          that.setData({
            isAuditMode: false,
          })
        }
        
      },
      complete: function() {
        wx.hideLoading();
        that.setData({
          is_show: false,
        })
      },
    });
  },

  click(e) {
    let index = e.currentTarget.dataset.index;
    let { opts, member_card_settings: list = [], money, moneyNum = '' } = this.data;
    this.setData({
      selected: index,
      money: index != -1 ? '' : money,
      moneyNum: index != -1 ? '' : moneyNum,
      id: opts.from ? list[index].id : '',
      price: opts.from ? list[index].price : '',
      decs: opts.from ? list[index].dec : '',
    });
  },

  pay() {
    let that = this;
    let data = {};
    let selected = Number(that.data.selected);
    let opts = that.data.opts;
    if (opts.from) {
      if (selected === -1) {
        wx.showModal({
          title: '提示',
          content: '请选择购买VIP会员卡的种类',
          showCancel: false,
        });
        return;
      }
      data.id = that.data.id;
      data.shop_id = 0;
    } else {
      if (selected === -1) {
        let money = that.data.money.trim();
        if (money === '') {
          wx.showModal({
            title: '提示',
            content: '请输入充值金额',
            showCancel: false,
          });
          return;
        } else if (money < 0.01) {
          wx.showModal({
            title: '提示',
            content: '充值金额不能小于0.01',
            showCancel: false,
          });
          return;
        }
        data.pay_price = money;
        data.send_price = 0;
      } else if (selected === -2) {
        wx.showModal({
          title: '提示',
          content: '请选择充值金额',
          showCancel: false,
        });
        return;
      } else {
        let list = that.data.list;
        data.pay_price = list[selected].pay_price;
        data.send_price = list[selected].send_price;
      }
      if (!data.pay_price) {
        wx.showModal({
          title: '提示',
          content: '请选择充值金额',
          showCancel: false,
        });
        return;
      }
    }

    data.pay_type = 'WECHAT_PAY';
    that.setData({
      disabled: true,
    });
    wx.showLoading({
      title: '提交中',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: opts.from ? api.user.buy_member_card : api.recharge.submit,
      data: data,
      method: 'POST',
      success: function(res) {
        if (res.code === 0) {
          wx.hideLoading();
          let hints = res.hints || '充值成功';
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            complete: function(e) {
              that.setData({
                disabled: false,
              });
              if (
                e.errMsg === 'requestPayment:fail' ||
                e.errMsg === 'requestPayment:fail cancel'
              ) {
                wx.showModal({
                  title: '提示',
                  content: '订单尚未支付',
                  showCancel: false,
                  confirmText: '确认',
                });
                return;
              }
              if (e.errMsg === 'requestPayment:ok') {
                wx.showModal({
                  title: '提示',
                  content: opts.from ? '支付成功' : hints,
                  showCancel: false,
                  confirmText: '确认',
                  success: function() {
                    wx.navigateBack();
                  },
                });
              }
            },
          });
          return;
        } else {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false,
          });
          that.setData({
            disabled: false,
          });
          wx.hideLoading();
        }
      },
    });
  },

  bindInput(e) {
    if (Number(this.data.selected) === -1) {
      this.setData({
        money: e.detail.value.indexOf('￥') > -1 ? (e.detail.value.split('￥')[1] > 0 ? (e.detail.value.split('￥')[1]).replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') : '') : e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'),
        moneyNum: e.detail.value.indexOf('￥') > -1 ? (e.detail.value.split('￥')[1] > 0 ? '￥' + (e.detail.value.split('￥')[1]).replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') : '') : '￥' + (e.detail.value).replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'),
      });
    }
  },

  collectFormId(e) {
    app.collectFormId(e);
  },
});
